package edu.mju.eduservice.controller;


import edu.mju.commonutils.ReturnCode;
import edu.mju.eduservice.entity.StudyChapter;
import edu.mju.eduservice.service.StudyChapterService;
import edu.mju.eduservice.vo.chapter.ChapterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiaoxin
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/eduservice/study-chapter")
public class StudyChapterController {

    @Autowired
    private StudyChapterService studyChapterService;

    /**
     *  课程大纲列表
     * @return
     */
    @GetMapping("/getChapter/{courseId}")
    public ReturnCode getChapter(@PathVariable String courseId){
        List<ChapterVo> list = studyChapterService.getChapterByCourseId(courseId);
        Map<String,Object> map =  new HashMap<>();
        map.put("Chapter",list);
        return ReturnCode.success().data(map);
    }

    /**
     *  添加章节
     * @param studyChapter
     * @return
     */
    @PostMapping("/addChapter")
    public ReturnCode addChapter(@RequestBody StudyChapter studyChapter){
        studyChapterService.save(studyChapter);
        return ReturnCode.success();
    }

    /**
     *  根据章节id查询
     * @param chapterId
     * @return
     */
    @GetMapping("/getChapterInfo/{chapterId}")
    public ReturnCode getChapterInfo(@PathVariable String chapterId){
        StudyChapter studyChapter = studyChapterService.getById(chapterId);
        Map<String,Object> map = new HashMap<>();
        map.put("chapter",studyChapter);
        return ReturnCode.success().data(map);
    }

    /**
     * 修改章节
     * @param studyChapter
     * @return
     */
    @PostMapping("/updateChapterInfo")
    public ReturnCode updateChapterInfo(@RequestBody StudyChapter studyChapter){
        studyChapterService.updateById(studyChapter);
        return ReturnCode.success();
    }

    /**
     * 删除章节：如果有小节，无法删除
     * @param chapterId
     * @return
     */
    @DeleteMapping("/deleteChapter/{chapterId}")
    public ReturnCode deleteChapter(@PathVariable String chapterId){
        boolean flag = studyChapterService.deleteChapter(chapterId);
        if(flag){
            return ReturnCode.success();
        }else {
            return ReturnCode.fail();
        }

    }
}

